// 包装函数
// 在单位时间内多次触发，只会执行一次函数
function debounce(fn, delay) {
  let timer = null
  let last = null
  return function (...args) {
    if (timer) {
      clearTimeout(timer)
      timer = null
    }
    const that = this
    const duration = delay - new Date().getTime() + last
    if (duration > 0) {
      timer = setTimeout(() => {
        fn.apply(that, args)
        timer = null
        last = new Date().getTime()
      }, duration)
    } else {
      fn.apply(that, args)
      last = new Date().getTime()
    }
  }
}

